package com.mall507.fivepeaks.widget.dashboardview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;


/**
 * @description 坐标系原点为View最左上角顶点<br />
 *              X++:右移<br />
 *              Y++:下移<br />
 *              角度系:时钟3点始,顺时针旋转一周角度递增
 * @author 贺子航   2015-03-23
 * **/
public class DashboardView extends View {

	private int itemNum=50;//仪表盘总刻度数量
	private int soAngle=230;//仪表盘总可见度数
	private int rectWindth=25;//旋转矩形宽
	private int rectHeight=4;//旋转矩形高
	private float itemAngle;//仪表盘每个刻度间隔度数
	private int itemAngleStart;//仪表盘开始度数
	private int itemNumYes=50;//仪表盘亮灯刻度数量<=itemNum,>0
	
	public DashboardView(Context context) {
		super(context);
	}
	
	public DashboardView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		itemAngle=soAngle*1.0f/(itemNum-1);
		itemAngleStart=(soAngle-180)/2;
		//亮灯
		Paint p1 = new Paint();
		p1.setStyle(Paint.Style.FILL);//充满
		p1.setColor(Color.parseColor("#01E0B4"));
		p1.setAntiAlias(true);// 设置画笔的锯齿效果
		//不亮灯
		Paint p2 = new Paint();
		p2.setStyle(Paint.Style.FILL);//充满
		p2.setColor(Color.parseColor("#262930"));
		p2.setAntiAlias(true);// 设置画笔的锯齿效果
		
		RectF rectF = new RectF(getWidth()/2-rectWindth-getWidth()/8, -rectHeight, getWidth()/2-rectWindth-getWidth()/8+rectWindth, 0);
		//移动原点为View中心点
		canvas.translate(getWidth()/2,getHeight()*2/3);
		
		for(int i=itemNum;i>=1;i--){
			//从第itemNum个刻度算起
			if(i==itemNum){
				//定初始刻度的角度(最右边那个刻度)
				canvas.rotate(itemAngleStart);
			}else{
				//其它刻度的角度
				canvas.rotate(-(itemAngle*(itemNum-i+1)));
			}
			if(i<=itemNumYes){
				canvas.drawRoundRect(rectF, 4, 3, p1);
			}else{
				canvas.drawRoundRect(rectF, 4, 3, p2);
			}
			//回到原点角度
			canvas.rotate(itemAngle*(itemNum-i+1));
		}
	}

	/**
	 * 设置仪表盘亮灯刻度数量
	 * @param itemNumYes 0<总数量<=itemNum
	 * */
	public void setItemNumYes(int itemNumYes) {
		if(itemNumYes>itemNum){
			this.itemNumYes = itemNum;
		}else
		if(itemNumYes<0){
			this.itemNumYes = 0;
		}else{
			this.itemNumYes = itemNumYes;
		}
	}

	/**
	 * 设置仪表盘刻度总数量
	 * @param itemNum 0<数量<=50
	 * */
	public void setItemNum(int itemNum) {
		if(itemNum>50){
			this.itemNum = 50;
		} else
		if(itemNum<0){
			this.itemNum = 0;
		}else{
			this.itemNum = itemNum;
		}
	}
	
	public int getItemNum() {
		return itemNum;
	}

	public float getItemAngle() {
		return itemAngle;
	}

	public int getItemNumYes() {
		return itemNumYes;
	}

}
